
package net.swa.dewei.web.action;

import java.util.List;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Required;
import net.swa.dewei.beans.entity.Battery;
import net.swa.dewei.beans.entity.BatteryCheXingRelation;
import net.swa.dewei.beans.entity.CheXing;
import net.swa.dewei.service.BatteryService;
import net.swa.system.beans.entity.Dict;
import net.swa.system.service.ICommonService;
import net.swa.system.util.annotation.Log;
import net.swa.system.web.action.AbstractBaseAction;
import net.swa.util.DateUtils;
import net.swa.util.JsonResult;

/**电池信息表 Action**/
public class BatteryAction extends AbstractBaseAction
{
    private static final long serialVersionUID = -9167064623097370397L;
    
    private final Logger log = Logger.getLogger(BatteryAction.class);

    /** 公用方法接口 */
    private ICommonService commonService;

    private BatteryService batteryService;

    private Battery model;

    /**电池类型**/
    private List<Dict> dianchiTypes;

    /**电池品牌**/
    private List<Dict> dianchiPinPais;

    //实体类id
    private Long id;

    //id集合
    private Long[] ids;

    private List<CheXing> cheXingList;

    /**初始化分页查询**/
    @Log(description = "初始化电池分页查询")
    public String listPage()
    {
        dianchiTypes = commonService.search("title", "电池型号", Dict.class, null, "dictPaixu", "asc");
        dianchiPinPais = commonService.search("title", "电池品牌", Dict.class, null, "dictPaixu", "asc");
        return "listPage";
    }

    /**去新增或者修改页面**/
    @Log(description = "去新增或者修改电池页面")
    public String toAddDianChi() throws Exception
    {
        dianchiTypes = commonService.search("title", "电池型号", Dict.class, null, "dictPaixu", "asc");
        dianchiPinPais = commonService.search("title", "电池品牌", Dict.class, null, "dictPaixu", "asc");
        cheXingList = commonService.search(null, null, CheXing.class, null, "sort", "asc");
        if (null != id)
        {
            model = batteryService.queryById(id);
            //用ids保存已经选中的车型
            if (null != model)
            {
                if (null != model.getRelationList() && (model.getRelationList().size() > 0))
                {
                    ids = new Long[model.getRelationList().size()];
                    int i = 0;
                    for (BatteryCheXingRelation r : model.getRelationList())
                    {
                        ids[i] = r.getChexingId();
                        i++;
                    }
                }
            }
        }
        else
        {
            model = new Battery();
        }
        return "toAddDianChi";
    }

    /**新增或者修改**/
    @Log(description = "新增或者修改电池")
    public String saveDianChi() throws Exception
    {
        boolean flag;
        if (null == model.getId())
        {
            log.debug("新增电池："+model);
            model.setCrateDate(DateUtils.getCurrDate(null));
            flag = batteryService.add(model, ids);
        }
        else
        {
            log.debug("修改电池："+model);
            model.setUpdateDate(DateUtils.getCurrDate(null));
            flag = batteryService.update(model, ids);
        }
        if (flag)
        {
            outSuccess();
        }
        else
        {
            outError("该品牌型号电池已经存在");
        }
        return null;
    }

    /** * 删除方法**/
    @Log(description = "删除电池")
    public String delete() throws Exception
    {
        batteryService.delete(ids);
        outSuccess();
        return null;
    }

    /***
     * 根据车型选择电池(？)
     * 若没有车型查询所有，但限制条数
     * @throws Exception 
     */
    public String ajaxQueryDianchi() throws Exception
    {
        JsonResult<Battery> json;
        json = batteryService.queryDianchiByCheXingId(id);
        json.setSuccess(true);
        outJson(json);
        return null;
    }

    @Required
    @Resource
    public void setCommonService(ICommonService commonService)
    {
        this.commonService = commonService;
    }

    public Long getId()
    {

        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public Long[] getIds()
    {
        return ids;
    }

    public void setIds(Long[] ids)
    {
        this.ids = ids;
    }

    public Battery getModel()
    {
        return model;
    }

    public void setModel(Battery model)
    {
        this.model = model;
    }

    @Required
    @Resource
    public void setBatteryService(BatteryService batteryService)
    {
        this.batteryService = batteryService;
    }

    public List<CheXing> getCheXingList()
    {
        return cheXingList;
    }

    public void setCheXingList(List<CheXing> cheXingList)
    {
        this.cheXingList = cheXingList;
    }

    public List<Dict> getDianchiTypes()
    {
        return dianchiTypes;
    }

    public void setDianchiTypes(List<Dict> dianchiTypes)
    {
        this.dianchiTypes = dianchiTypes;
    }

    public List<Dict> getDianchiPinPais()
    {
        return dianchiPinPais;
    }

    public void setDianchiPinPais(List<Dict> dianchiPinPais)
    {
        this.dianchiPinPais = dianchiPinPais;
    }

    public ICommonService getCommonService()
    {
        return commonService;
    }
}
